package com.eunion.core.common.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;

import com.eunion.core.common.jpa.FlushControl;
import com.eunion.core.JsonHelper;
import com.eunion.core.common.domain.AbstractExcelReport;
import com.eunion.core.common.service.ExcelImportService;

public abstract class AbstractExcelImportService<T extends AbstractExcelReport> implements ExcelImportService<T> {

    @Autowired
    protected EntityManager em;

    @Autowired
    protected JsonHelper json;

    @Override
    public void save(List<T> dataList, FlushControl flushControl) {
        AtomicInteger count = new AtomicInteger();
        dataList.forEach((entity) -> {
            em.persist(entity);
            flushControl.flush(em, count.incrementAndGet());
        });
        em.flush();
    }

}
